package drivers;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import commands.UserEventGroup;
import commands.UserMDGroup;
import net.base.annotations.NotNull;
import net.protocol.carfun.common.CommandResponse;
import net.socket.driver.mdc.MDCommandDriver;
import net.socket.exception.ClientConnectException;
import net.socket.exception.CommandExecException;
import net.socket.exception.ProtocolUnSupportException;
import net.socket.thread.SocketProxy;
import register.RountRegisterInfo;

import java.util.List;
import java.util.Map;

/**
 * @Author: Qiao Hang
 * @CreateDate: 2021/3/25 下午6:47
 * @UpdateDate:
 * @Description:
 */
public class PointMDCommandDriver implements MDCommandDriver<UserMDGroup.Points, RountRegisterInfo> {

    @Override
    public CommandResponse<JSON> chooseAndDeal(@NotNull UserMDGroup.Points points,
                                               @NotNull SocketProxy<RountRegisterInfo> socketProxy,
                                               @NotNull Map<String, String> headers,
                                               JSON json) throws ProtocolUnSupportException, CommandExecException, ClientConnectException {
        CommandResponse res = null;
        switch (points){
            case POINT_ADD:
                res = dealPointAdd(points,socketProxy,headers,json);
                break;
            case POINT_DESC:
                res = dealPointDesc(points,socketProxy,headers,json);
                break;
            case POINT_EMPTY:
                res = dealPointEmpty(points,socketProxy,headers,json);
                break;
        }
        return res;
    }

    //不要求上行参数，清空
    private CommandResponse dealPointEmpty(UserMDGroup.Points points,
                                           SocketProxy<RountRegisterInfo> socketProxy,
                                           Map<String, String> headers,
                                           JSON json) throws CommandExecException {
        socketProxy.getRegisterInfo().getRoutPoints().clear();
        //发送清空的消息通知, 默认处于绑定态registerInfo  channel 均不为null.
        socketProxy.getChannel().push(socketProxy, UserEventGroup.Points.EVENT_POINT_EMPTY,new JSONArray());
        return CommandResponse.success(null);
    }

    private CommandResponse dealPointDesc(UserMDGroup.Points points,
                                          SocketProxy<RountRegisterInfo> socketProxy,
                                          Map<String, String> headers,
                                          JSON json) throws CommandExecException {
        List<String> routPoints = socketProxy.getRegisterInfo().getRoutPoints();
        for (Object o : JSONArray.class.cast(json)) {
            routPoints.remove(o);
        }
        JSONObject data = new JSONObject() {{
            put("left", routPoints);
            put("remove", json);
        }};
        //发送清空的消息通知, 默认处于绑定态registerInfo  channel 均不为null.
        socketProxy.getChannel().push(socketProxy, UserEventGroup.Points.EVENT_POINT_DESC,data);
        return CommandResponse.success(socketProxy.getRegisterInfo().getRoutPoints());
    }

    private CommandResponse dealPointAdd(UserMDGroup.Points points,
                                         SocketProxy<RountRegisterInfo> socketProxy,
                                         Map<String, String> headers,
                                         JSON json) throws CommandExecException {
        List<String> routPoints = socketProxy.getRegisterInfo().getRoutPoints();
        for (Object o : JSONArray.class.cast(json)) {
            routPoints.add(o.toString());
        }
        JSONObject data = new JSONObject() {{
            put("afterAdd", routPoints);
            put("add", json);
        }};
        //发送清空的消息通知, 默认处于绑定态registerInfo  channel 均不为null.
        socketProxy.getChannel().push(socketProxy, UserEventGroup.Points.EVENT_POINT_ADD,data);
        return CommandResponse.success(socketProxy.getRegisterInfo().getRoutPoints());
    }

    @Override
    public UserMDGroup.Points[] getSupportCommands() {
        return UserMDGroup.Points.values();
    }

}
